#!/srv/newsblur/venv/newsblur3/bin/python

"""
Load average plugin for Munin.

Based on the default "load" plugin in Munin

First tries reading from /proc/loadavg if it exists. Otherwise, execute
`uptime` and parse out the load average.
"""

import os

from vendor.munin import MuninPlugin


class LoadAVGPlugin(MuninPlugin):
    title = "Load average"
    args = "--base 1000 -l 0"
    vlabel = "load"
    scale = False
    category = "system"

    @property
    def fields(self):
        warning = os.environ.get('load_warn', 10)
        critical = os.environ.get('load_crit', 120)
        return [("load", dict(
                label = "load",
                info = 'The load average of the machine describes how many processes are in the run-queue (scheduled to run "immediately").',
                type = "GAUGE",
                min = "0",
                warning = str(warning),
                critical = str(critical)))]

    def execute(self):
        if os.path.exists("/proc/loadavg"):
            loadavg = open("/proc/loadavg", "r").read().strip().split(' ')
        else:
            from subprocess import PIPE, Popen
            output = Popen(["uptime"], stdout=PIPE).communicate()[0]
            loadavg = output.rsplit(':', 1)[1].strip().split(' ')[:3]
        return dict(load=loadavg[1])

if __name__ == "__main__":
    LoadAVGPlugin().run()
